package com.chart.web.controller.chart.sta;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.domian.dto.ChartDateTto;
import com.chart.chart.service.IChartConfigsService;
import com.chart.common.core.controller.BaseController;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.ExcelDataUtils;
import com.chart.common.utils.ExcelUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 饼图
 *
 * @author chart
 */
@RestController
@RequestMapping("/pie/chart")
public class PieChartController extends BaseController
{


    @Autowired
    private IChartConfigsService chartConfigsService;


    //南丁格尔图、扇形图，圆角环形图
    @PostMapping("/nightingale/importData")
    public AjaxResult importData(@RequestBody ChartDateTto chartDateTto) throws Exception {


        return null;
    }


/*
    //南丁格尔图、扇形图，圆角环形图
    @PostMapping("/nightingale/importData")
    public AjaxResult importData(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<List<Object>> newList = new ArrayList<>();


            for (int i = 1; i < getdata.size(); i++) {
                List<String> strings = getdata.get(i);
                if (!CollectionUtils.isEmpty(strings)) {
                    List<Object> list = new ArrayList<>();
                    for (int i1 = 0; i1 < strings.size(); i1++) {
                        String s = strings.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            if (i==1) {
                                //数据中的格式：集合的第一个必须是[name，value]
                                list.add("name");
                                list.add("value");
                            }else {
                                if (i1==0) {
                                    list.add(s);
                                }else {
                                    list.add(Double.parseDouble(s));
                                }
                            }
                        }
                    }
                    newList.add(list);
                }
            }

            List<Map> maps = new ArrayList<>();

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());


                HashMap<String, Object> map = new HashMap<>();
                map.put("source", newList);
                maps.add(map);
            jsonObject.put("dataset", maps);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }
*/



    //南丁格尔图、扇形图，圆角环形图
    @PostMapping("/nightingale/v2/importData")
    public AjaxResult v2importData(@RequestBody List<Map<String, Object>> dataList
            ,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
         /*   workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);*/

            Sheet sheet = ExcelUtils.convertDataListToSheetRemoveNineAndOne(dataList);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<List<Object>> newList = new ArrayList<>();


            for (int i = 1; i < getdata.size(); i++) {
                List<String> strings = getdata.get(i);
                if (!CollectionUtils.isEmpty(strings)) {
                    List<Object> list = new ArrayList<>();
                    for (int i1 = 0; i1 < strings.size(); i1++) {
                        String s = strings.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            if (i==1) {
                                //数据中的格式：集合的第一个必须是[name，value]
                                list.add("name");
                                list.add("value");
                            }else {
                                if (i1==0) {
                                    list.add(s);
                                }else {
                                    list.add(Double.parseDouble(s));
                                }
                            }
                        }
                    }
                    newList.add(list);
                }
            }

            List<Map> maps = new ArrayList<>();

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());


            HashMap<String, Object> map = new HashMap<>();
            map.put("source", newList);
            maps.add(map);
            jsonObject.put("dataset", maps);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }
}
